import amesu from "../../model/bot/amesu";
import { Bot_msg } from "../../model/bot/bot_msg";
import browser, { tempDir } from "../../model/browser/browser";
import server from "../../model/client/server";
import func from "../../model/func";
import { mathType, packge } from "../../model/msgHandle/packge";
import { prop_min_type } from "../../shared/interface";
import { ServiceType } from "../../shared/master/serviceProto";
import { style_default } from "../../style/default";

class index extends packge {

    constructor() {
        super()
    }
    init(): void {
        this.addMath(mathType.math_all, '主神商店', ((msg: Bot_msg) => { this.list(msg) }), this)
        this.addMath(mathType.math_start, '查看商品', ((msg: Bot_msg) => { this.look(msg) }), this)
        this.addMath(mathType.math_start, '购买商品', ((msg: Bot_msg) => { this.buy(msg) }), this)

        this.addMath(mathType.math_all, '技能商店', ((msg: Bot_msg) => { this.list_v2(msg, 'shop/skill/List') }), this)
        this.addMath(mathType.math_start, '查看装备商店', ((msg: Bot_msg) => { this.look_v2(msg, 'shop/equip/Look') }), this, false)
        this.addMath(mathType.math_start, '购买装备', ((msg: Bot_msg) => { this.buy_v2(msg, 'shop/equip/Buy') }), this, false)


        this.addMath(mathType.math_all, '装备商店', ((msg: Bot_msg) => { this.list_v2(msg, 'shop/equip/List') }), this)
        this.addMath(mathType.math_start, '查看技能商店', ((msg: Bot_msg) => { this.look_v2(msg, 'shop/skill/Look') }), this, false)
        this.addMath(mathType.math_start, '购买技能', ((msg: Bot_msg) => { this.buy_v2(msg, 'shop/skill/Buy') }), this, false)
    }
    async list_v2<T extends keyof ServiceType['api']>(msg: Bot_msg, path: T) {
        server.api(path, {}, msg);
    }
    async buy_v2<T extends keyof ServiceType['api']>(msg: Bot_msg, path: T) {
        if (this.getNumber(msg.getContent(true)) == 0) {
            msg.repeText(`购买商品:购买商品 + ID`)
            return;
        }
        let res = await server.api(path, {
            idx: this.getNumber(msg.getContent(true))
        }, msg);
        if (!res) {
            return;
        }
    }
    async look_v2<T extends keyof ServiceType['api']>(msg: Bot_msg, path: T) {
        if (this.getNumber(msg.getContent(true)) == 0) {
            msg.repeText(`商品查看:查看商品 + ID`)
            return;
        }
        let res = await server.api(path, {
            idx: this.getNumber(msg.getContent(true))
        }, msg);
        if (!res) {
            return;
        }
    }

    async buy(msg: Bot_msg) {
        if (this.getNumber(msg.getContent(true)) == 0) {
            msg.repeText(`购买商品:购买商品 + ID`)
            return;
        }
        let res = await server.api('shop/Buy', {
            idx: this.getNumber(msg.getContent(true))
        }, msg);
        if (!res) {
            return;
        }
    }
    async list(msg: Bot_msg) {
        let res = await server.api('shop/List', {}, msg);
        if (!res) {
            return;
        }
        this.renderBag(msg, res.list, res.nextTime)
    }
    async look(msg: Bot_msg) {
        if (this.getNumber(msg.getContent(true)) == 0) {
            msg.repeText(`商品查看:查看商品 + ID`)
            return;
        }
        let res = await server.api('shop/Look', {
            idx: this.getNumber(msg.getContent(true))
        }, msg);
        if (!res) {
            return;
            
        }
    }
    private renderBag(msg: Bot_msg, list: any[], updateTime: number) {
        let style = new style_default();
        let _list = [];
        style.setTitle('主神商店')
        style.addLine(`下次补货时间:${func.getTimeStr(updateTime)}`)
        for (let i = 0; i < list.length; i++) {
            const element = list[i];
            const idx = i;
            if (!element) {
                continue;
            }
            if (element.type == prop_min_type.装备 || element.cont == 1) {
                style.addLine(`[${idx + 1}]${element.name}💰️${func.BN(element.price)}`)
            } else {
                style.addLine(`[${idx + 1}]${element.name}X${element.cont}💰️${func.BN(element.price * element.cont)}`)
            }
        }
        // browser.render(tempDir.通用文字, {
        //     list:_list
        // }).then((png) => {
        //     amesu.sendLocaImage(msg.getFormId(),png);
        // });
        style.addLine(`🔎查看:查看商品 + ID`)
        style.addLine(`💰购买:购买商品 + ID`)
        style.toimg(msg.getFormId())
    }
}
export default new index();